function _saveImg(imgUrl) {
	uni.downloadFile({
		url: imgUrl,
		success: function(res) {
			uni.saveImageToPhotosAlbum({
				filePath: res.tempFilePath,
				success: (res) => {
					wx.showToast({
						title: '保存成功',
					})
				},
				fail: (err) => {
					console.info(err)
					if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
						err.errMsg === "saveImageToPhotosAlbum:fail auth deny" ||
						err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
						uni.showModal({
							title: "提示",
							content: "需要您授权保存到相册",
							showCancel: false,
							success: () => {
								uni.openSetting({
									success: (resAuth) => {
										if (resAuth.authSetting[
												'scope.writePhotosAlbum'
											]) {
											uni.showModal({
												title: "提示",
												content: "获取权限成功，再次点击图片即可保存",
												showCancel: false
											})
										} else {
											uni.showModal({
												title: "提示",
												content: "获取权限失败，将无法保存到相册",
												showCancel: false
											})
										}
									}
								})
							}
						})
					} else {
						uni.showToast({
							title: '保存失败',
							icon: "error"
						})
					}
				}
			})
		},
		fail: (err) => {
			uni.showToast({
				title: '保存图片有误',
			})
		}
	})
}

const util = {
	saveImage(imgUrl) {
		uni.getSetting({
			success(res) {
				if (res.authSetting['scope.writePhotosAlbum']) {
					_saveImg(imgUrl);
				} else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
					uni.authorize({
						scope: 'scope.writePhotosAlbum',
						success() {
							_saveImg(imgUrl);
						},
						fail() {
							uni.showToast({
								title: '您没有授权，无法保存到相册',
								icon: 'none'
							})
						}
					})
				} else {
					uni.openSetting({
						success(res) {
							if (res.authSetting['scope.writePhotosAlbum']) {
								_saveImg(imgUrl);
							} else {
								uni.showToast({
									title: '您没有授权，无法保存到相册',
									icon: 'none'
								})
							}
						}
					})
				}
			}
		})
	}
}

export default util;